package com.sky.aspect;
import com.sky.constant.PasswordConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;

import java.lang.reflect.Field;
import java.time.LocalDateTime;

@Component
@Aspect
@Order(2)
public class AddAspect {
    @Around("@annotation(com.sky.annotation.AddAnnotation)")
    public Object enhanceAdd(ProceedingJoinPoint pjp) throws Throwable {
        Object[] args = pjp.getArgs();
        Object obj = args[0];
        Class<?> clazz = obj.getClass();
        Field declaredField = clazz.getDeclaredField("createTime");
        Field declaredField1 = clazz.getDeclaredField("updateTime");
        Field declaredField2 = clazz.getDeclaredField("createUser");
        Field declaredField3 = clazz.getDeclaredField("updateUser");
        Field declaredField4 = clazz.getDeclaredField("status");
        declaredField.setAccessible(true);
        declaredField.set(obj, LocalDateTime.now());
        declaredField1.setAccessible(true);
        declaredField1.set(obj, LocalDateTime.now());
        declaredField2.setAccessible(true);
        declaredField2.set(obj, BaseContext.getCurrentId());
        declaredField3.setAccessible(true);
        declaredField3.set(obj, BaseContext.getCurrentId());
        declaredField4.setAccessible(true);
        declaredField4.set(obj, StatusConstant.ENABLE);
        Object proceed = pjp.proceed(args);
        return proceed;
    }
}
